#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "tfrmstruct.h"
#include "tlrsyntaxhighlighter.h"
#include "tdlgconfig.h"
#include "tfrmcalibunit.h"
#include "tdlgcalib.h"
#include <QHBoxLayout>
#include <QSplitter>
#include <QStyleFactory>
#include <QDesktopWidget>
#include <QFileDialog>
#include <QMimeData>
#include <QSerialPortInfo>
#include <QTimer>
#include <QMessageBox>
#include <QFileDialog>
#include <QScrollArea>
#include <QDateTime>
#include <QToolTip>
#include <QPainter>
#include <QGroupBox>
#include <QListWidget>
#include <QDesktopServices>
#include <QTextCodec>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    //UI初始化
    {
        ui->setupUi(this);
        m_pix_empty_file.load(":/images/image_empty_file.png");
        m_tabWidget->clear();
        ui->actionOpenFile->setText(tr("打开文件"));
        setAcceptDrops(true);
        ui->lbSerialPortStatus->setPixmap(m_pixmapSerialPortClose);

        ui->leParaStaAdd->setMinimumWidth(60);
        ui->leParaStaAdd->setMaximumWidth(60);
        ui->leDevPort->setMinimumWidth(60);
        ui->leDevPort->setMaximumWidth(60);
        ui->leSerialAddr->setMinimumWidth(60);
        ui->leSerialAddr->setMaximumWidth(60);
        ui->leDevIP->setText(TLR::devIPFromINI());
        ui->leDevPort->setText("2305");
        ui->leEquIdStr->setPlaceholderText(tr("请输入设备识别字"));
        ui->leParaStaAdd->setText("0");
        ui->leSerialAddr->setText("0");
        ui->cbBaud_NetToSerial->addItem("1200",1200);
        ui->cbBaud_NetToSerial->addItem("2400",2400);
        ui->cbBaud_NetToSerial->addItem("4800",4800);
        ui->cbBaud_NetToSerial->addItem("9600",9600);
        ui->cbBaud_NetToSerial->addItem("19200",19200);
        ui->cbBaud_NetToSerial->addItem("38400",38400);
        ui->cbBaud_NetToSerial->addItem("57600",57600);
        ui->cbBaud_NetToSerial->addItem("115200",11);
        ui->btnRd->setMinimumSize(80,30);
        ui->btnRd->setMaximumSize(80,30);
        ui->btnWr->setMinimumSize(80,30);
        ui->btnWr->setMaximumSize(80,30);
        ui->btnCtl->setMinimumSize(80,30);
        ui->btnCtl->setMaximumSize(80,30);
        ui->btnOpenCloseSerialPort->setMinimumSize(80,30);
        ui->btnOpenCloseSerialPort->setMaximumSize(80,30);
        ui->btnUpdateSerialPort->setMinimumSize(80,30);
        ui->btnUpdateSerialPort->setMaximumSize(80,30);
        //组装右边操作栏
        QWidget *pwgtcontent = new QWidget;
        QVBoxLayout *pvblcontent = new QVBoxLayout;
        pvblcontent->addWidget(m_tabWidget);
        pvblcontent->addWidget(ui->wgtOptPanel);
        pwgtcontent->setLayout(pvblcontent);
        ui->wgtOptPanel->setMinimumHeight(120);
        ui->wgtOptPanel->setMaximumHeight(120);
        pvblcontent->setContentsMargins(0,0,0,0);
        //组装主页面
        QHBoxLayout *phblcontent = new QHBoxLayout;
        phblcontent->addWidget(pwgtcontent);
        ui->centralwidget->setLayout(phblcontent);
        //居中显示
        int nwidth = 600;
        int nheight = 750;
        QDesktopWidget *pwgtdesktop = QApplication::desktop();
        if(pwgtdesktop) {
            setGeometry((pwgtdesktop->width()-nwidth) / 2, (pwgtdesktop->height()-nheight)/2,nwidth,nheight);
        }
        QStringList nserialbaudlist = {"1200","2400","4800","9600","19200","38400","57600","115200","921600"};
        ui->cbBaud->addItems(nserialbaudlist);
        ui->spbAutoReadInterval->setMinimum(100);
        ui->spbAutoReadInterval->setMaximum(60*1000);
        ui->leEquIdStr->setReadOnly(true);
        ui->leDevPort->setReadOnly(true);
        ui->leParaStaAdd->setReadOnly(true);
        //文件是否修改指示
        m_actUpdateFile = new QAction;
        m_actUpdateFile->setIcon(QIcon(":/images/image_file_modify_hint.png"));
        ui->menubar->addAction(m_actUpdateFile);
        m_actUpdateFile->setVisible(false);
        //图片提示
        m_lbImgHint = new QLabel(m_tabWidget);
        m_lbImgHint->setPixmap(m_pix_empty_file);
        m_lbImgHint->setVisible(false);
        //状态栏
        m_lbCommType = new QLabel;
        m_lbCommType->setText("透传模式");
        ui->statusBar->addWidget(ui->lbToolTip,1);
        ui->statusBar->addWidget(m_lbCommType,0);
        slotUpdateCommHint("软件启动",false);

        TLR::get()->m_lrStructModel.loadFromFile(TLR::get()->m_filePath);
        m_fileSystemWatcher.addPath(TLR::get()->m_filePath);
        setWindowTitle(VERSION_STR + " " + TLR::get()->m_filePath);
        dataToUI(TUIType::uiFrame);
        dataToUI(TUIType::uiSerialPort);

        qApp->setStyleSheet("file:///:/qss/haetstyle.css");

        m_tabWidget->installEventFilter(this);
    }
    //信号槽
    {
        //翻页切换
        connect(m_tabWidget,&QTabWidget::currentChanged,this,&MainWindow::slotTabCurrentChanged);
        //读取按钮点击
        connect(ui->btnRd,&QPushButton::clicked,this,&MainWindow::slotBtnRdClicked);
        //下发按钮点击
        connect(ui->btnWr,&QPushButton::clicked,this,&MainWindow::slotBtnWrClicked);
        //控制按钮点击
        connect(ui->btnCtl,&QPushButton::clicked,this,&MainWindow::slotBtnCtlClicked);
        //打开关闭按钮点击
        connect(ui->btnOpenCloseSerialPort,&QPushButton::clicked,this,&MainWindow::slotBtnOpenCloseSerialPortClicked);
        //刷新串口按钮点击
        connect(ui->btnUpdateSerialPort,&QPushButton::clicked,this,&MainWindow::slotBtnUpdateSerialPortClicked);
        //自动读取
        m_tmAutoRead = new QTimer(this);
        connect(m_tmAutoRead,&QTimer::timeout,this,&MainWindow::slotTmAutoReadTimeOut);
        //自动读取勾选框状态改变
        connect(ui->chkAutoRead,&QCheckBox::stateChanged,this,&MainWindow::slotChkAutoReadStateChanged);
        //网络配置按钮点击
        connect(ui->actNetConfig,&QAction::triggered,this,&MainWindow::slotActNetConfigTrigger);
        //导入/导出
        connect(ui->actImport,&QAction::triggered,this,&MainWindow::slotActImportTrigger);
        connect(ui->actExport,&QAction::triggered,this,&MainWindow::slotActExportTrigger);
        //数据示波器按钮点击
        connect(ui->actWave,&QAction::triggered,this,&MainWindow::slotActWaveTrigger);
        //信号槽_文件监视器
        connect(&m_fileSystemWatcher,&QFileSystemWatcher::fileChanged,this,&MainWindow::slotFileChanged);
        //信号槽_从文件中更新
        connect(m_actUpdateFile,&QAction::triggered,this,&MainWindow::slotActUpdateFileTriggered);
        //信号槽_更新通讯提示
        connect(TLR::get(),&TLR::sigUpdateCommHint,this,&MainWindow::slotUpdateCommHint);
        //信号槽_校准流程中需要进行的一系列读取/下发
        connect(TLR::get(),&TLR::sigCalibRdPara,this,&MainWindow::slotCalibRdPara);
        connect(TLR::get(),&TLR::sigCalibWrPara,this,&MainWindow::slotCalibWrPara);
        //信号槽_波形页面销毁
        connect(TLR::get(),&TLR::sigDlgWaveDestroy,this,&MainWindow::slotDlgWaveDestroy);

    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

/*
 * UI操作
 * nuitype : UI操作类型
 */
void MainWindow::dataToUI(const TUIType &nuitype)
{
    switch (nuitype) {
    //刷新界面
    case TUIType::uiFrame: {
        //清空tab
        QWidget *pwgttab = m_tabWidget->currentWidget();
        while(pwgttab) {
            m_tabWidget->removeTab(0);
            pwgttab->deleteLater();
            pwgttab = m_tabWidget->currentWidget();
        }
        //添加tab
        auto &nlrstructmodel = TLR::get()->m_lrStructModel;
        for(int i=0;i<nlrstructmodel.m_lrstructModel.size();i++) {
            TLRStruct lrStruct = nlrstructmodel.getStruct(i);
            if(!lrStruct.visible) {
                continue;
            }
            TFrmStruct *pfrmstruct = new TFrmStruct;
            pfrmstruct->reset(i);
            m_tabWidget->addTab(pfrmstruct,nlrstructmodel.getStruct(i).name);
        }
        TFrmStruct *cur_frm_struct = qobject_cast<TFrmStruct*>(m_tabWidget->currentWidget());
        if(cur_frm_struct) {
            ui->leParaStaAdd->setText(QString::number(nlrstructmodel.getStruct(cur_frm_struct->getIndex()).paraStaAdd));
        }
        //添加按钮tab
        auto &cmdButtonModel = nlrstructmodel.m_cmdButtonModel;
        if(!cmdButtonModel.isEmpty()) {
            QWidget *wgtCmdButton = new QWidget;
            int buttonSize = nlrstructmodel.m_cmdButtonModel.size();
            int colCount = 4;
            int rowCount = buttonSize / colCount + (buttonSize % colCount?1:0);
            int buttonIndex;
            QVBoxLayout *vblButton = new QVBoxLayout;
            for(int buttonRowIndex=0;buttonRowIndex<rowCount;buttonRowIndex++) {
                QHBoxLayout *hblButton = new QHBoxLayout;
                for(int buttonColIndex=0;buttonColIndex<colCount;buttonColIndex++) {
                    buttonIndex = buttonRowIndex*colCount + buttonColIndex;
                    if(buttonIndex < cmdButtonModel.size()) {
                        TLRCmdButton &cmdButton = cmdButtonModel[buttonIndex];
                        QPushButton *btn = new QPushButton;
                        connect(btn,&QPushButton::clicked,this,&MainWindow::slotCmdButtonClicked);
                        btn->setText(cmdButton.name);
                        btn->setMinimumHeight(30);
                        btn->setMaximumHeight(30);
                        hblButton->addWidget(btn);
                        cmdButton.btn = btn;
                    }
                    else {
                        QWidget *wgtBlank = new QWidget;
                        hblButton->addWidget(wgtBlank);
                    }
                }
                vblButton->addLayout(hblButton);
            }
            vblButton->addStretch(1);
            wgtCmdButton->setLayout(vblButton);
            wgtCmdButton->setProperty("border",true);
            QScrollArea *scrCmdButton = new QScrollArea;
            scrCmdButton->setObjectName("scrollCmdBtn");
            scrCmdButton->setWidget(wgtCmdButton);
            m_tabWidget->addTab(scrCmdButton,tr("指令按钮"));
        }
        //添加校准tab
        if(!nlrstructmodel.m_calibModel.isEmpty()) {
            QListWidget *lvCalib = new QListWidget;
            lvCalib->setObjectName("lvCalib");
            for(int i=0;i<nlrstructmodel.m_calibModel.size();i++) {
                TFrmCalibUnit *calibUnit = new TFrmCalibUnit;
                calibUnit->reset(nlrstructmodel.m_calibModel.at(i));
                calibUnit->setID(i);
                QListWidgetItem *item = new QListWidgetItem;
                item->setSizeHint(QSize(100,80));
                lvCalib->addItem(item);
                lvCalib->setItemWidget(item,calibUnit);
            }
            m_tabWidget->addTab(lvCalib,"校准");
        }
        //无tab提示
        m_lbImgHint->setVisible(m_tabWidget->count() == 0);
        //设备识别字
        ui->leEquIdStr->setText(nlrstructmodel.getEquidStr());
        //通讯方式
        switch (nlrstructmodel.getCommType()) {
        case TCommType::ctNet:
            ui->wgtNetPanel->setVisible(true);
            ui->wgtSerialPanel->setVisible(false);
            ui->wgtNetToSerial->setVisible(false);
            m_lbCommType->setText("网络模式");
            break;
        case TCommType::ctSerial:
            ui->wgtNetPanel->setVisible(false);
            ui->wgtSerialPanel->setVisible(true);
            ui->wgtNetToSerial->setVisible(false);
            m_lbCommType->setText("串口模式");
            break;
        case TCommType::ctNetToSerial:
            ui->wgtNetPanel->setVisible(false);
            ui->wgtSerialPanel->setVisible(false);
            ui->wgtNetToSerial->setVisible(true);
            m_lbCommType->setText("透传模式(未支持)");
            break;
        default:
            break;
        }
        //设备UDP端口
        ui->leDevPort->setText(QString::number(nlrstructmodel.getUDPPort()));
        //串口波特率
        if(ui->cbBaud->isEnabled()) {
            int baudIndex = ui->cbBaud->findText(QString::number(nlrstructmodel.getUartBaud()));
            if(baudIndex >= 0) {
                ui->cbBaud->setCurrentIndex(baudIndex);
            }
        }
        //串口地址
        ui->leSerialAddr->setText(QString::number(nlrstructmodel.getUartAddr()));
        break;
    }
        //串口信息
    case TUIType::uiSerialPort: {
        ui->cbSerialPort->clear();
        QString nportname = TLR::get()->getCurrentPortName();
        auto nserialportinfos = QSerialPortInfo::availablePorts();
        int nindex = 0;
        for(int i=0;i<nserialportinfos.size();i++) {
            QSerialPortInfo nserialportinfo = nserialportinfos.at(i);
            ui->cbSerialPort->addItem(nserialportinfo.description() + " (" + nserialportinfo.portName() + ")",nserialportinfo.portName());
            if(nserialportinfo.portName() == nportname) {
                nindex = i;
            }
        }
        ui->cbSerialPort->setCurrentIndex(nindex);
        break;
    }
    }
}

/*
 * 拖拽进入事件
 */
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
    if(event->mimeData()->urls().size() == 1) {
        QString nfilepath = event->mimeData()->urls().front().toString();
        if(nfilepath.endsWith(".json")) {
            event->setAccepted(true);
        }
    }
}

/*
 * 拖拽放下事件
 */
void MainWindow::dropEvent(QDropEvent *event)
{
    if(event->mimeData()->urls().size() == 1) {
        QString nfilepath = event->mimeData()->urls().front().toLocalFile();
        m_fileSystemWatcher.removePath(TLR::get()->m_filePath);
        m_fileSystemWatcher.addPath(nfilepath);
        TLR::get()->m_lrStructModel.loadFromFile(nfilepath);
        TLR::get()->saveFilePath(nfilepath);
        setWindowTitle(VERSION_STR + " " + TLR::get()->m_filePath);
        dataToUI(TUIType::uiFrame);
    }
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event)
    TLR::devIPToINI(ui->leDevIP->text());
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if(obj == m_tabWidget) {
        if(event->type() == QEvent::Resize) {
            QResizeEvent *resizeEvent = static_cast<QResizeEvent*>(event);
            int w = resizeEvent->size().width();
            int h = resizeEvent->size().height();
            m_lbImgHint->move((w - m_lbImgHint->width()) / 2,(h - m_lbImgHint->height()) / 2);
        }
    }
    return QWidget::eventFilter(obj,event);
}

/*
 * 翻页切换
 */
void MainWindow::slotTabCurrentChanged(int index)
{
    Q_UNUSED(index)
    QWidget *curr_page = m_tabWidget->currentWidget();
    if(!curr_page) {
        return;
    }
    QString objectName = curr_page->objectName();
    //指令按钮页面
    if(objectName == "scrollCmdBtn") {
        ui->chkAutoRead->setChecked(false);
        ui->wgtToolBar1->setVisible(false);
    }
    //校准页面
    else if(objectName == "lvCalib") {
        ui->chkAutoRead->setChecked(false);
        ui->wgtToolBar1->setVisible(false);
    }
    //结构体页面
    else {
        TFrmStruct *frmStruct = qobject_cast<TFrmStruct*>(curr_page);
        if(frmStruct) {
            TLRStruct lrStruct = TLR::get()->m_lrStructModel.getStruct(frmStruct->getIndex());
            ui->leParaStaAdd->setText(QString::number(lrStruct.paraStaAdd));
        }
        ui->wgtToolBar1->setVisible(true);
    }
}

/*
 * 读取按钮点击
 */
void MainWindow::slotBtnRdClicked()
{
    TFrmStruct *pfrmstruct = qobject_cast<TFrmStruct*>(m_tabWidget->currentWidget());
    if(pfrmstruct) {
        slotUpdateCommHint("正在读取...",false);
        switch (TLR::get()->m_lrStructModel.getCommType()) {
        case TCommType::ctNet:
            pfrmstruct->rdPara(ui->leEquIdStr->text(),ui->leParaStaAdd->text().toInt(),QHostAddress(ui->leDevIP->text()),ui->leDevPort->text().toInt(),ui->leSerialAddr->text().toInt());
            break;
        case TCommType::ctSerial:
            pfrmstruct->rdParaSerial(ui->leEquIdStr->text(),ui->leParaStaAdd->text().toInt(),ui->leSerialAddr->text().toInt());
            break;
        case TCommType::ctNetToSerial:
            //TODO
            break;
        default:
            break;
        }
    }
}

/*
 * 下发按钮点击
 */
void MainWindow::slotBtnWrClicked()
{
    TFrmStruct *pfrmstruct = qobject_cast<TFrmStruct*>(m_tabWidget->currentWidget());
    if(pfrmstruct) {
        slotUpdateCommHint("正在下发...",false);
        switch (TLR::get()->m_lrStructModel.getCommType()) {
        case TCommType::ctNet:
            pfrmstruct->wrPara(ui->leEquIdStr->text(),ui->leParaStaAdd->text().toInt(),QHostAddress(ui->leDevIP->text()),ui->leDevPort->text().toInt(),ui->leSerialAddr->text().toInt());
            break;
        case TCommType::ctSerial:
            pfrmstruct->wrParaSerial(ui->leEquIdStr->text(),ui->leParaStaAdd->text().toInt(),ui->leSerialAddr->text().toInt());
            break;
        case TCommType::ctNetToSerial:
            //TODO
            break;
        }
    }
}

/*
 * 控制按钮点击
 */
void MainWindow::slotBtnCtlClicked()
{
    bool ret;
    QString error_str;
    TFrmStruct *pfrmstruct = qobject_cast<TFrmStruct*>(m_tabWidget->currentWidget());
    if(pfrmstruct) {
        switch (TLR::get()->m_lrStructModel.getCommType()) {
        case TCommType::ctNet:
            ret = pfrmstruct->ctlPara_Single(ui->leEquIdStr->text(),ui->leParaStaAdd->text().toInt(),QHostAddress(ui->leDevIP->text()),ui->leDevPort->text().toInt(),ui->leSerialAddr->text().toInt(),error_str);
            if(!ret) {
                QMessageBox::warning(this,"提示",error_str);
            }
            else {
                slotUpdateCommHint("正在控制...",false);
            }
            break;
        case TCommType::ctSerial:
            QMessageBox::warning(this,"提示","此模式暂不支持串口");
            break;
        case TCommType::ctNetToSerial:
            QMessageBox::warning(this,"提示","此模式暂不支持透传");
            break;
        }
    }
}

/*
 * 串口刷新点击
 */
void MainWindow::slotBtnUpdateSerialPortClicked()
{
    dataToUI(TUIType::uiSerialPort);
}

/*
 * 串口打开关闭按钮点击
 */
void MainWindow::slotBtnOpenCloseSerialPortClicked()
{
    //关闭
    if(ui->btnOpenCloseSerialPort->text() == tr("关闭")) {
        TLR::get()->closeSerialPort();
        ui->btnOpenCloseSerialPort->setText(tr("打开"));
        ui->lbSerialPortStatus->setPixmap(m_pixmapSerialPortClose);
        ui->cbSerialPort->setEnabled(true);
        ui->cbBaud->setEnabled(true);
        ui->btnUpdateSerialPort->setEnabled(true);
    }
    //打开
    else if(ui->btnOpenCloseSerialPort->text() == tr("打开")) {
        bool nret = TLR::get()->openSerialPort(ui->cbSerialPort->currentData().toString(),ui->cbBaud->currentText().toInt());
        if(nret) {
            ui->btnOpenCloseSerialPort->setText(tr("关闭"));
            ui->lbSerialPortStatus->setPixmap(m_pixmapSerialPortOpen);
            ui->cbSerialPort->setEnabled(false);
            ui->cbBaud->setEnabled(false);
            ui->btnUpdateSerialPort->setEnabled(false);
        }
    }
}

/**
 * @brief 自动读取
 */
void MainWindow::slotTmAutoReadTimeOut()
{
    slotBtnRdClicked();
}

void MainWindow::slotChkAutoReadStateChanged(int state)
{
    if(state == Qt::Checked) {
        m_tmAutoRead->start(ui->spbAutoReadInterval->value());
        ui->spbAutoReadInterval->setReadOnly(true);
    }
    else if(state == Qt::Unchecked) {
        m_tmAutoRead->stop();
        ui->spbAutoReadInterval->setReadOnly(false);
    }
}

/**
 * @brief 指令按钮点击
 */
void MainWindow::slotCmdButtonClicked()
{
    auto &buttonModel = TLR::get()->m_lrStructModel.m_cmdButtonModel;
    for(int i=0;i<buttonModel.size();i++) {
        const TLRCmdButton &cmdButton = buttonModel.at(i);
        if(cmdButton.btn == sender()) {
            TCommType commType = TLR::get()->m_lrStructModel.getCommType();
            if(commType == TCommType::ctNet) {
                QHostAddress ip;
                int port;
                if(cmdButton.ip.isEmpty()) {
                    ip = QHostAddress(ui->leDevIP->text());
                }
                else {
                    ip = QHostAddress(cmdButton.ip);
                }
                if(cmdButton.port == -1) {
                    port = ui->leDevPort->text().toInt();
                }
                else {
                    port = cmdButton.port;
                }
                TLR::get()->udpSockAnyWrite(cmdButton.cmd.toUtf8(),ip,port);
            }
            else if(commType == TCommType::ctSerial) {
                TLR::get()->serialPortWrite(cmdButton.cmd.toUtf8(),cmdButton.cmd.toUtf8().size());
            }
            break;
        }
    }
}

/**
 * @brief 网络配置按钮触发
 */
void MainWindow::slotActNetConfigTrigger()
{
    TDlgConfig *dlgConfig = new TDlgConfig;
    dlgConfig->exec();
    dlgConfig->deleteLater();
}

/**
 * @brief 执行导入操作
 */
void MainWindow::slotActImportTrigger()
{
    TFrmStruct *frmStruct = qobject_cast<TFrmStruct*>(m_tabWidget->currentWidget());
    if(!frmStruct) {
        QMessageBox::warning(this,tr("提示"),tr("请先选择需要导入的结构体"));
        return;
    }
    frmStruct->importData();
}

/**
 * @brief 执行导出操作
 */
void MainWindow::slotActExportTrigger()
{
    TFrmStruct *frmStruct = qobject_cast<TFrmStruct*>(m_tabWidget->currentWidget());
    if(!frmStruct) {
        QMessageBox::warning(this,tr("提示"),tr("请先选择需要导出的结构体"));
        return;
    }
    frmStruct->exportData();
}

/**
 * @brief 文件发生修改
 * @param path
 */
void MainWindow::slotFileChanged(const QString &path)
{
    Q_UNUSED(path);
    m_actUpdateFile->setVisible(true);
    slotUpdateCommHint("文件发生变化，请点击小红点同步",false);
}

/**
 * @brief 从文件中更新
 */
void MainWindow::slotActUpdateFileTriggered()
{
    TLR::get()->m_lrStructModel.loadFromFile(TLR::get()->m_filePath);
    dataToUI(TUIType::uiFrame);
    m_actUpdateFile->setVisible(false);
    slotUpdateCommHint("文件同步完成",false);
}

/**
 * @brief 更新通讯提示
 * @param text
 */
void MainWindow::slotUpdateCommHint(const QString &text,bool needMessageBox)
{
    ui->lbToolTip->setText(QString("[%1] %2").arg(QTime::currentTime().toString("hh:mm:ss.zzz"),text));
    if(needMessageBox) {
        QMessageBox::information(this,"提示",text);
    }
}

/**
 * @brief 校准流程中需要进行的一系列读取
 * @param sta 起始
 * @param len 长度
 */
void MainWindow::slotCalibRdPara(int sta,int len)
{
    TCommType commType = TLR::get()->m_lrStructModel.getCommType();
    if(commType == ctNet) {
        auto sndData = TLR::rdPara(ui->leEquIdStr->text(),sta,len,ui->leSerialAddr->text().toInt());
        TLR::get()->udpSockAnyWrite(sndData,QHostAddress(ui->leDevIP->text()),ui->leDevPort->text().toInt());
        TLR::devIPToINI(ui->leDevIP->text());
    }
    else if(commType == ctSerial) {

    }
    else if(commType == ctNetToSerial) {
        //TODO
    }
}

/**
 * @brief 校准流程中需要进行的一系列下发
 * @param sta 起始
 * @param len 长度
 * @param type 类型
 * @param val 值
 */
void MainWindow::slotCalibWrPara(int sta, int len, const QString &type, s64 val)
{
    TCommType commType = TLR::get()->m_lrStructModel.getCommType();
    if(commType == ctNet) {
        if(type == "u16") {
            u16 wr_val = val;
            auto sndData = TLR::wrPara(ui->leEquIdStr->text(),sta,len,&wr_val,ui->leSerialAddr->text().toInt());
            TLR::get()->udpSockAnyWrite(sndData,QHostAddress(ui->leDevIP->text()),ui->leDevPort->text().toInt());
        }
        else if(type == "s16") {
            s16 wr_val = val;
            auto sndData = TLR::wrPara(ui->leEquIdStr->text(),sta,len,&wr_val,ui->leSerialAddr->text().toInt());
            TLR::get()->udpSockAnyWrite(sndData,QHostAddress(ui->leDevIP->text()),ui->leDevPort->text().toInt());
        }
        else if(type == "u32") {
            u32 wr_val = val;
            auto sndData = TLR::wrPara(ui->leEquIdStr->text(),sta,len,&wr_val,ui->leSerialAddr->text().toInt());
            TLR::get()->udpSockAnyWrite(sndData,QHostAddress(ui->leDevIP->text()),ui->leDevPort->text().toInt());
        }
        else if(type == "s32") {
            s32 wr_val = val;
            auto sndData = TLR::wrPara(ui->leEquIdStr->text(),sta,len,&wr_val,ui->leSerialAddr->text().toInt());
            TLR::get()->udpSockAnyWrite(sndData,QHostAddress(ui->leDevIP->text()),ui->leDevPort->text().toInt());
        }
    }
    else if(commType == ctSerial) {

    }
    else if(commType == ctNetToSerial) {
        //TODO
    }
}

/**
 * @brief 数据示波器按钮点击
 */
void MainWindow::slotActWaveTrigger()
{
    if(m_dlgWave) {
        return;
    }
    m_dlgWave = new TDlgWave(this);
    m_dlgWave->show();
}

/**
 * @brief 波形页面销毁
 */
void MainWindow::slotDlgWaveDestroy()
{
    m_dlgWave = nullptr;
}

